// pages/details/details.js
var app = getApp()
const $api = require('../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '详情', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    examineStatus: '', //店铺状态 1 待初审  2 待复审 3 已审核 4 初审驳回 5 复审驳回 6 已发货
    examineText: '', //驳回原因
    cancle_status: 0, //审核人员进入 是否为退款状态 cancle_status 0 未取消 1 待初审 2待退款  3 待复审 4 待退款  5 已取消
    licenseState: 0, //营业执照是否可以修改  1==修改 0==不可以
    licenseStateText: '修改',
    applyShop: {},
    companySH: ['省', '市', '区'],
    regionIndex: [0, 0, 0], //地址省市区默认下标
    payIdx: 0, //1==全款支付 2==分批支付
    payNone: 1, //1==可以分批支付 0==不可以分批支付
    payPrice: '', //分批支付输入金额
    payText: '', //输入金额提示
    payTextQuan: '全款支付',
    payTextFen: '分批付款',
    payModelText: '支付'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var _this = this;
    _this.setData({
      loadModal: true,
      is_admin: wx.getStorageSync('is_admin'),
      shop_id: options.shop_id,
      options: options
    })
    //审核人员进入 1 待初审  2 待复审 3 已审核 4 初审驳回 5 复审驳回
    if (options.status) {
      _this.setData({
        examineStatus: options.status
      })
    }
    //审核人员进入 是否为退款状态 cancle_status 0 未取消 1 待初审 2待退款 3 待复审 4 待退款  5 已取消
    if (options.cancle_status) {
      _this.setData({
        cancle_status: options.cancle_status
      })
    }
    var is_detail = 0;
    if (options.type == 1) {
      is_detail = 1
    }
    this.setData({
      is_detail
    })
    $api.getApplyShopDetail({
      login_token: wx.getStorageSync('login_token'),
      shop_id: options.shop_id,
      has_order: 1,
      is_detail: is_detail,
    }).then((res) => {
      var details = res.data.data;
      var applyShop = res.data.data.shop_info;
      //获取地址
      $api.getArea({
        login_token: wx.getStorageSync('login_token'),
      }).then((res) => {
        _this.setData({
          addressJson: res.data.data,
          details: details,
          applyShop: applyShop,
          loadModal: false
        })
      }).catch((err) => {
        wx.showToast({
          title: '地址获取失败',
          icon: 'none',
          duration: 2000
        })
      })
      if (_this.data.examineStatus == '') {
        _this.setData({
          examineStatus: res.data.data.shop_info.status
        })
      }
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })

    $api.getShopConfig({
      login_token: wx.getStorageSync('login_token'),
      shop_id: _this.data.shop_id,
    }).then((res) => {
      if (parseFloat(res.data.data.left_money) >= parseFloat(res.data.data.pay_limit)) {
        _this.setData({
          payNone: 1,
        })
      } else {
        _this.setData({
          payNone: 0
        })
      }

      if (parseFloat(res.data.data.yet_money) > 0) {
        _this.setData({
          payTextFen: "继续分批付款",
          payTextQuan: "支付剩余金额",
          payModelText: '继续支付'
        })
      }

      _this.setData({
        payData: res.data.data,
      })
    }).catch((err) => {

    })

  },
  //修改
  onModify(e) {
    wx.redirectTo({
      url: '/pages/reading/reading?step=1&state=2&shop_id=' + e.currentTarget.dataset.shop_id + '&dzy=' + e.currentTarget.dataset.dzy + '&pzimg=1',
    })
  },
  // 选择是否分批付款
  onDivideModal(e) {
    var _this = this;
    var payidx = e.currentTarget.dataset.payidx;
    _this.setData({
      payIdx: payidx,
      payPrice: '',
      payText: ''
    })
  },
  //获取分批输入的支付金额
  onDividePrice(e) {
    this.setData({
      payPrice: e.detail.value,
    })
  },
  //支付
  onWxPay() {
    var _this = this;
    var yet_money = parseFloat(_this.data.payData.yet_money)
    $api.getApplyShopDetail({
      login_token: wx.getStorageSync('login_token'),
      shop_id: _this.data.options.shop_id,
      has_order: 1,
      is_detail: _this.data.is_detail,
    }).then((res) => {
      var details = res.data.data;
      var applyShop = res.data.data.shop_info;
      if (_this.data.payIdx == 1) {
        //全款支付
        if (details.order_info.is_fpfk == 0) {
          _this.onPay({
            login_token: wx.getStorageSync('login_token'),
            order_id: details.shop_info.order_id,
          })
        } else {
          _this.onPay({
            login_token: wx.getStorageSync('login_token'),
            order_id: details.shop_info.order_id,
            is_fp: 1,
            fp_money: _this.data.payData.left_money
          })
        }


      } else if (_this.data.payIdx == 2 && _this.data.payNone != 0) {
        //分批付款
        var pay_limit = parseFloat(_this.data.payData.pay_limit);
        var payPrice = parseFloat(_this.data.payPrice);
        var left_money = parseFloat(_this.data.payData.left_money)

        if (_this.data.payPrice == '') {
          _this.setData({
            payText: '输入支付金额'
          })
        } else if (payPrice < pay_limit) {
          _this.setData({
            payText: '最低支付' + pay_limit + '元'
          })
        } else if (payPrice > left_money) {
          _this.setData({
            payText: '输入支付金额超出'
          })
        } else {
          _this.setData({
            payText: ''
          })
          _this.onPay({
            login_token: wx.getStorageSync('login_token'),
            order_id: details.shop_info.order_id,
            is_fp: 1,
            fp_money: payPrice
          })



        }


      } else {
        wx.showToast({
          title: '请选择支付方式',
          icon: 'none',
          duration: 2000
        })
      }
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })



  },
  //支付调取
  onPay(data) {
    var _this = this;
    $api.wxPay(data).then((res) => {
      console.log(res)
      var wxPay = res.data.data;
      var timeStamp = wxPay.timeStamp
      wx.requestPayment({
        timeStamp: timeStamp.toString(),
        nonceStr: wxPay.nonceStr,
        package: wxPay.package,
        signType: wxPay.signType,
        paySign: wxPay.paySign,
        success(res) {
          console.log(res)
          wx.redirectTo({
            url: '/pages/index/index',
          })
        },
        fail(res) {
          console.log(res)

          $api.dealFpCancle({
            login_token: wx.getStorageSync('login_token'),
            fp_out_trade_no: wxPay.fp_out_trade_no
          }).then((res) => {
            _this.setData({
              loadModal: false
            })

          }).catch((err) => {
            _this.setData({
              loadModal: false
            })
          })
        }
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  //图片上传
  onUpload(e) {
    var _this = this
    //上传图片
    var cancle_status = _this.data.cancle_status;
    if (cancle_status == 0) {
      $api.uploadImages().then((res) => {
        console.log(res)
        var image_url = res.url
        //更新店铺图片
        $api.updateShopImage({
          login_token: wx.getStorageSync('login_token'),
          shop_id: _this.data.shop_id,
          image_type: e.currentTarget.dataset.applyimg,
          image_url: image_url
        }).then((res) => {
          if (e.currentTarget.dataset.applyimg == 1) {
            //营业执照照片
            _this.setData({
              ['details.shop_info.license_img']: image_url,
            })
          } else if (e.currentTarget.dataset.applyimg == 2) {
            //房本或租赁协议照片
            _this.setData({
              ['details.shop_info.house_img']: image_url,
            })
          } else if (e.currentTarget.dataset.applyimg == 3) {
            //门头照片
            _this.setData({
              ['details.shop_info.door_img']: image_url,
            })
          }
        }).catch((err) => {

        })
      }).catch((err) => {
        console.log(err)
      })
    }


  },
  //删除图片提示框
  onCloseImg(e) {
    var _this = this;
    var applyimg = e.currentTarget.dataset.applyimg;
    _this.setData({
      modalName: 'imageModal',
      applyimg: applyimg
    })
  },
  //弹框
  showModal(e) {
    var _this = this;
    _this.setData({
      modalName: e.currentTarget.dataset.target
    })

  },
  //弹框隐藏
  hideModal(e) {
    this.setData({
      modalName: null,
      examineText: '',
      regionIndex: [0, 0, 0],
    })
  },
  //删除店铺图片
  onDelShopImage() {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    $api.delShopImage({
      login_token: wx.getStorageSync('login_token'),
      shop_id: _this.data.shop_id,
      image_type: _this.data.applyimg
    }).then((res) => {
      if (_this.data.applyimg == 1) {
        //营业执照照片
        _this.setData({
          ['details.shop_info.license_img']: '',
        })
      } else if (_this.data.applyimg == 2) {
        //房本或租赁协议照片
        _this.setData({
          ['details.shop_info.house_img']: '',
        })
      } else if (_this.data.applyimg == 3) {
        //门头照片
        _this.setData({
          ['details.shop_info.door_img']: '',
        })
      }
      _this.setData({
        loadModal: false,
        modalName: null
      })
    }).catch((err) => {
      _this.setData({
        loadModal: false,
        modalName: null
      })
    })





  },
  //获取驳回原因
  onExamineText(e) {
    this.setData({
      examineText: e.detail.value
    })
  },
  //确认驳回
  onRejectModel() {
    var _this = this;
    if (_this.data.examineText == '') {
      wx.showToast({
        title: '请输入驳回原因',
        icon: 'none',
        duration: 2000
      })
    } else {
      var cancle_status = _this.data.cancle_status;
      _this.setData({
        loadModal: true
      })
      if (cancle_status == 0) {
        $api.checkShop({
          login_token: wx.getStorageSync('login_token'),
          shop_id: _this.data.shop_id,
          type: 2,
          reject_reason: _this.data.examineText
        }).then((res) => {
          _this.setData({
            loadModal: false,
            modalName: null
          })
          wx.showToast({
            title: '驳回成功',
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 1500)
        }).catch((err) => {
          _this.setData({
            loadModal: false
          })
        })
      } else {
        $api.refundCheck({
          login_token: wx.getStorageSync('login_token'),
          shop_id: _this.data.shop_id,
          type: 2,
          reject_reason: _this.data.examineText
        }).then((res) => {
          _this.setData({
            loadModal: false,
            modalName: null
          })
          wx.showToast({
            title: '驳回成功',
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 1500)
        }).catch((err) => {
          _this.setData({
            loadModal: false
          })
        })
      }


    }

  },
  //通过申请
  onAdopt() {
    var _this = this;
    var cancle_status = _this.data.cancle_status;
    _this.setData({
      loadModal: true
    })
    if (cancle_status == 0) {
      $api.checkShop({
        login_token: wx.getStorageSync('login_token'),
        shop_id: _this.data.shop_id,
        type: 1
      }).then((res) => {
        _this.setData({
          loadModal: false,
          modalName: null
        })
        wx.showToast({
          title: '审核通过',
          icon: 'none',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 1500)
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    } else {
      $api.refundCheck({
        login_token: wx.getStorageSync('login_token'),
        shop_id: _this.data.shop_id,
        type: 1
      }).then((res) => {
        _this.setData({
          loadModal: false,
          modalName: null
        })
        wx.showToast({
          title: '审核通过',
          icon: 'none',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1,
          })
        }, 1500)
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })
    }


  },
  //查看图片
  onSeeImage(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: [e.currentTarget.dataset.url]
    })
  },
  //发货
  onFahuo() {
    var _this = this;
    _this.setData({
      loadModal: true
    })
    $api.fahuo({
      login_token: wx.getStorageSync('login_token'),
      shop_id: _this.data.details.shop_info.id
    }).then((res) => {
      _this.setData({
        loadModal: false
      })
      wx.showToast({
        title: '发货成功',
        icon: 'none',
        duration: 2000
      })
      setTimeout(function () {
        wx.navigateBack({
          delta: 1,
        })
      }, 1500)
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })



  },
  //查看物流
  onLogistics(e) {
    wx.navigateTo({
      url: '/pages/logistics/logistics?express_id=' + e.currentTarget.dataset.id + '&express_name=' + e.currentTarget.dataset.express_name + '&express_no=' + e.currentTarget.dataset.express_no,
    })
  },
  //选择是否有营业执照
  onHasLicense(e) {
    var _this = this;
    var haslicense = e.currentTarget.dataset.haslicense;
    if (haslicense == 2) {
      _this.setData({
        ['applyShop.company_address']: '',
        ['applyShop.company_city']: '',
        ['applyShop.company_name']: '',
        ['applyShop.company_provice']: '',
        ['applyShop.register_date']: '',
        ['applyShop.social_credit_code']: '',
        // ['details.shop_info.license_img']: '',
      })
    }
    _this.setData({
      ['applyShop.has_license']: e.currentTarget.dataset.haslicense,
    })
    console.log(_this.data.applyShop.register_date)
  },
  //选择营业日期
  onLicenseTime: function (e) {
    this.setData({
      ['applyShop.register_date']: e.detail.value
    })
  },
  //地点底部弹出
  onSHG(e) {
    var _this = this;
    _this.setData({
      modalName: e.currentTarget.dataset.target,
    })

  },
  //滑动时获取地址index
  onAddressIndex(e) {
    console.log(e)
    var _this = this;
    _this.setData({
      regionIndex: e.detail.value
    })


  },
  //确认地址
  confirmAddress() {
    var _this = this;
    var regionIndex = _this.data.regionIndex;
    _this.setData({
      ['companySH[0]']: _this.data.addressJson[regionIndex[0]].provinceName,
      ['applyShop.company_provice']: _this.data.addressJson[regionIndex[0]].provinceId,
      ['companySH[1]']: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].cityName,
      ['applyShop.company_city']: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].cityId,
      ['companySH[2]']: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].region[regionIndex[2]].regionName,
      ['applyShop.company_area']: _this.data.addressJson[regionIndex[0]].city[regionIndex[1]].region[regionIndex[2]].regionId,
      modalName: null,
      regionIndex: [0, 0, 0]
    })
  },
  //修改营业执照
  onSwitchLicense() {
    var _this = this;
    var licenseState = _this.data.licenseState;
    if (licenseState == 0) {
      _this.setData({
        licenseState: 1,
        licenseStateText: '确认提交',
      })
      if (_this.data.details.shop_info.company_province_name != '') {
        _this.setData({
          ['companySH[0]']: _this.data.applyShop.company_province_name,
          ['companySH[1]']: _this.data.applyShop.company_city_name,
          ['companySH[2]']: _this.data.applyShop.company_area_name,
        })
      }
      _this.setData({
        has_license: 2,
      })
    } else {
      var applyShop = _this.data.applyShop;
      var data = {}
      if (applyShop.has_license == 2) {
        data = {
          login_token: wx.getStorageSync('login_token'),
          shop_id: _this.data.shop_id,
          has_license: 2,
        }
      } else {
        data = {
          login_token: wx.getStorageSync('login_token'),
          shop_id: _this.data.shop_id,
          has_license: 1,
          image_url: _this.data.details.shop_info.license_img,
          social_credit_code: applyShop.social_credit_code,
          register_date: applyShop.register_date,
          company_name: applyShop.company_name,
          company_provice: applyShop.company_provice,
          company_city: applyShop.company_city,
          company_address: applyShop.company_address,
          company_area: applyShop.company_area,
        }
      }
      _this.setData({
        loadModal: true
      })
      console.log(data)
      $api.updateShopLicense(data).then((res) => {
        _this.setData({
          licenseState: 0,
          licenseStateText: '修改',
          loadModal: false,
        })
        _this.onLoad(_this.data.options)
      }).catch((err) => {
        _this.setData({
          loadModal: false
        })
      })


    }

  },
  //获取营业执照详情文本内容
  onLicenseTxt(e) {
    var _this = this;
    var id = e.currentTarget.dataset.id;
    if (id == 1) {
      _this.setData({
        ['applyShop.social_credit_code']: e.detail.value
      })
    }
    if (id == 2) {
      _this.setData({
        ['applyShop.company_name']: e.detail.value
      })
    }
    if (id == 3) {
      _this.setData({
        ['applyShop.company_address']: e.detail.value
      })
    }
  },
  // 登记
  onCheckin(e) {
    let _this = this
    let data = {
      login_token: wx.getStorageSync('login_token'),
      shop_id: _this.data.shop_id
    }

    $api.shopcheckin(data).then((res) => {
      if (res.data.code == 0) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
        setTimeout((e) => {
          wx.navigateTo({
            url: '/pages/index/index',
          })
        }, 1000)

      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }

    }).catch((err) => {
    })
  },
  goUpLevel() {
    wx.removeStorageSync('userAddressData');
    wx.removeStorageSync('giftId');
    wx.removeStorageSync('wait_pay');
    wx.removeStorageSync('applyShop');
    wx.removeStorageSync('pay');
    wx.removeStorageSync('goodsList');
    wx.removeStorageSync('address');
    wx.removeStorageSync('default_lb_num');
    wx.removeStorageSync('giftnumber');
    wx.removeStorageSync('statusReading');
    wx.removeStorageSync('UpLevelType');
    wx.removeStorageSync('faultnum');
    wx.removeStorageSync('imglist');
    wx.removeStorageSync('giftInfoList');
    wx.removeStorageSync('addressid');
    // wx.setStorageSync('addressid', _this.data.details.id)
    wx.redirectTo({
      url: '/pages/reading/reading?UpLevelType=1&state=1&step=1&shop_id=' + this.data.shop_id,
    })
  }
})